/*
Copyright (c) 2010 julien barbay <barbay.julien@gmail.com>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

package martian.m4gic.display
{
	import flash.display.*;
	import flash.geom.Matrix;
	import flash.geom.Point;

	public class Graph
	{
		static private const RATIO:Number = 2 - Math.cos(Math.PI / 8);
		
		private var _data:Vector.<IGraphicsData>;
			public function get cmd():Vector.<IGraphicsData>
			{
				append();
				return _data;
			}
		
		private var _stroke:IGraphicsStroke;
		private var _fill:IGraphicsFill;
		private var _path:GraphicsPath;			
		
		private var _graphics:Graphics;
		
		public function Graph(graphics:Graphics = null)
		{
			_graphics = graphics;
			_data = new Vector.<IGraphicsData>;
		}
		
		public function stroke(weight:Number = 0.5, color:int = 0, alpha:Number = 1):Graph
		{
			if (weight <= 0) { return this; }
			else if (_path != null) { if (_path.commands.length > 0) { append(); } }
			
			_stroke = new GraphicsStroke(weight);
			(_stroke as GraphicsStroke).fill = new GraphicsSolidFill(color, alpha);
			
			return this;
		}
		
		public function fill(color:int = 0, alpha:Number = 1):Graph
		{
			if (_path != null) { if (_path.commands.length > 0) { append(); } }
			
			_fill = new GraphicsSolidFill(color, alpha);
			return this;
		}
		
		public function bitmap(bmd:BitmapData, full:Boolean = true, matrix:Matrix = null, repeat:Boolean = false, smooth:Boolean = true):Graph
		{
			if (_path != null) { if (_path.commands.length > 0) { append(); } }
			
			_fill = new GraphicsBitmapFill(bmd, matrix, repeat, smooth);
			if (full) { rect(bmd.width, bmd.height, matrix != null ? matrix.tx : 0, matrix != null ? matrix.ty : 0); }
			
			return this;
		}
		
		public function move(x:Number, y:Number):Graph
		{
			if (_path == null) { _path = new GraphicsPath(new Vector.<int>, new Vector.<Number>); }
			
			_path.commands.push(GraphicsPathCommand.MOVE_TO);
			_path.data.push(x, y);
			
			return this;
		}
		
		public function line(x:Number, y:Number):Graph
		{
			if (_path == null) { _path = new GraphicsPath(new Vector.<int>, new Vector.<Number>); }
			
			_path.commands.push(GraphicsPathCommand.LINE_TO);
			_path.data.push(x, y);
			
			return this;
		}
		
		public function curve(x:Number, y:Number, u:Number, v:Number):Graph
		{
			if (_path == null) { _path = new GraphicsPath(new Vector.<int>, new Vector.<Number>); }
			
			_path.commands.push(GraphicsPathCommand.CURVE_TO);
			_path.data.push(u, v, x, y);
			
			return this;
		}
		
		public function rect(width:Number, height:Number, x:Number = 0, y:Number = 0):Graph
		{
			move(x, y);
			line(x + width, y);
			line(x + width, y + height);
			line(x, y + height);
			line(x, y);
			
			return this;
		}
		
		public function square(side:Number, centered:Boolean = false, x:Number = 0, y:Number = 0):Graph
		{
			var offset:Number = centered ? side / 2 : 0;
			rect(side, side, x - offset, y - offset);
			
			return this;
		}

		public function ellipse(width:Number, height:Number, x:Number = 0, y:Number = 0):Graph
		{
			var a:Number = 0,
				w:Number = width / 2,
				h:Number = height / 2;
			
			x += w;
			y += h;
			
			move(x + w, y);
			for (var i:int = 1; i <= 8; i++)
			{
				a = i * Math.PI / 4;
				curve(x + w * Math.cos(a), y + h * Math.sin(a), x + w * RATIO * Math.cos(a - Math.PI / 8), y + h * RATIO * Math.sin(a - Math.PI / 8));
			}
			
			return this;
		}
		
		public function circle(radius:Number, centered:Boolean = true, x:Number = 0, y:Number = 0):Graph
		{
			var offset:Number = centered ? radius : 0;
			ellipse(radius * 2, radius * 2, x - offset, y - offset);
			
			return this;
		}
		
		/*
		public function plane(width:int, height:int, segments:int = 30, centered:Boolean = true, x:Number = 0, y:Number = 0):Graph
		{
			var points:Vector.<Vector.<Point>>	= new Vector.<Vector.<Point>>(),
				vertex:Vector.<Number>			= new Vector.<Number>(),
				indices:Vector.<int>			= new Vector.<int>(),
				uvdata:Vector.<Number>			= new Vector.<Number>(),
				offset:Point					= new Point();
				
			if (centered)
			{
				offset.x = - width / 2;
				offset.y = - height / 2;
			}
	
			var pa:Number, pb:Number;
			for (var a:int = 0; a < segments + 1; a++)
			{
				points[a] = new Vector.<Point>();
				pa = (a / segments) * width;

				for (var b:int = 0; b < segments + 1; b++)
				{
					pb = (b / segments) * height;
					points[a][b] = new Point(pa, pb).substract(offset);
				}
			}
		
			var p:Point, ux:Number, uy:Number, k:int = 0;
			for (var c:int = 0; c < points.length; c++)
			{
				for (var d:int = 0; y < points[c].length; d++)
				{
					p = points[c][d].clone();
					ux = c / segments;
					uy = d / segments;
					vertex.push(p.x);
					vertex.push(p.y);

					if (d < segments) { indices.push(k, k + segments + 1, k + 1, k + segments + 1, k + segments + 2, k + 1); }
					uvdata.push(ux, uy);
					++k;
				}
			}
		
			return this;	
		}
		*/
		
		public function append():Graph
		{
			if (_path == null) { move(0, 0); }
			if (_fill == null && _stroke == null) { fill(); }
			
			_data.push(_stroke, _fill, _path);
				_stroke = null;
				_fill = null;
				_path = null;
			
			return this;
		}
		
		protected function flush(all:Boolean = false):void
		{
			_data = null;
			_stroke = null;
			_fill = null;
			_path = null;
			
			if (!all) { _data = new Vector.<IGraphicsData>(); }
		}
		
		public function apply(graphics:Graphics = null, clear:Boolean = false, flush:Boolean = true):Graph
		{
			if (graphics == null) { graphics = _graphics; }
			if (graphics == null) { throw new Error('you cant draw on a null object'); }
			
			if (clear) { graphics.clear(); }
			graphics.drawGraphicsData(cmd);
			if (flush) { this.flush(); }
			
			return this;
		}
		
		public function release():void
		{
			flush(true);
			_data = null;
			_graphics = null;
		}
	}
}